﻿using System;
using System.Collections.Generic;
using System.Linq;
using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using StyleCopMaker.StyleCopMaker.Cleanup;

namespace StyleCopMaker.StyleCopMaker
{
    public class ApplyRules : IVsRunningDocTableEvents3
    {
        private RunningDocumentTable RunningDocumentTable { get; set; }

        private StyleCopMakerPackage StyleCopMakerPackage;

        /// <summary>
        /// Gets or sets an event cookie used as a notification token.
        /// </summary>
        private uint EventCookie { get; set; }

        public ApplyRules(StyleCopMakerPackage styleCopMakerPackage)
        {
            this.StyleCopMakerPackage = styleCopMakerPackage;

            CleanupGlobal.Init(styleCopMakerPackage);

            // Create and store a reference to the running document table.
            RunningDocumentTable = new RunningDocumentTable(styleCopMakerPackage);

            // Register with the running document table for events.
            EventCookie = RunningDocumentTable.Advise(this);
        }

        #region IVsRunningDocTableEvents3 Membres

        public int OnAfterAttributeChange(uint docCookie, uint grfAttribs)
        {
            return VSConstants.S_OK;
        }

        public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarchy pHierOld, uint itemidOld, string pszMkDocumentOld, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew)
        {
            return VSConstants.S_OK;
        }

        public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame)
        {
            return VSConstants.S_OK;
        }

        public int OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
        {
            return VSConstants.S_OK;
        }

        public int OnAfterSave(uint docCookie)
        {
            return VSConstants.S_OK;
        }

        public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
        {
            return VSConstants.S_OK;
        }

        public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
        {
            return VSConstants.S_OK;
        }

        public int OnBeforeSave(uint docCookie)
        {
            CleanupGlobal.CleanDocument(GetDocumentFromCookie(docCookie));

            return VSConstants.S_OK;
        }

        private Document GetDocumentFromCookie(uint docCookie)
        {
            // Retrieve document information from the cookie to get the full document name.
            var documentName = RunningDocumentTable.GetDocumentInfo(docCookie).Moniker;

            // Search against the IDE documents to find the object that matches the full document name.
            return StyleCopMakerPackage.IDE.Documents.OfType<Document>().FirstOrDefault(x => x.FullName == documentName);
        }

        #endregion IVsRunningDocTableEvents3 Membres
    }
}